# -*- coding:utf-8 -*-
class Solution:
    # matrix类型为二维列表，需要返回列表
    def printMatrix(self, matrix):
        # write code here
        h1 = 0
        l1 = 0
        h2 = len(matrix) - 1
        l2 = len(matrix[0]) - 1
        self.a = []
        while h1 <= h2 and l1 <= l2:
            self.help(matrix, h1, l1, h2, l2)
            h1 += 1
            l1 += 1
            h2 -= 1
            l2 -= 1
            print(self.a)
        return self.a
    def help(self, m, h1, l1, h2, l2):
        if h1 == h2:
            for i in range(l1,l2+1):
                self.a.append(m[h1][i])
        else:
            if l1 == l2:
                for i in range(h1,h2+1):
                    self.a.append(m[i][l1])
            else:
                curh = h1
                curl = l1
                while curl != l2:
                    self.a.append(m[h1][curl])
                    curl += 1
                while curh != h2:
                    self.a.append(m[curh][l2])
                    curh += 1
                while curl != l1:
                    self.a.append(m[h2][curl])
                    curl -= 1
                while curh != h1:
                    self.a.append(m[curh][l1])
                    curh -= 1




a= [[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15],[16,17,18,19,20],[21,22,23,24,25]]
s=Solution()
a1= s.printMatrix(a)
print(a1)